package com.example.chasshelper.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.LinearLayout;

import com.example.chasshelper.db.AppSQLiteHelper;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TeacherDao  {
    private OkHttpClient okHttpClient = OkHttpUtils.getClient();
    private String ipUrl = OkHttpUtils.getUrl();
    private String url;

    /*
    * 查询所有
    * */
    public List<Teacher> findAll() {
        url = ipUrl + "/teacher/findAll";
        System.out.println("url:"+url);
        List<Teacher> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Teacher>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /*
    * 添加
    * */
    public  void insert(Teacher teacher) {
        url = ipUrl + "/teacher/insert?teacherAccount=" + teacher.getTeacherAccount() + "&teacherPassword=" + teacher.getTeacherPassword() + "&teacherPhone=" + teacher.getTeacherPhone();
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

    /*
     * 根据id查询
     * */
    public  Teacher findById(Integer id) {
        url = ipUrl + "/teacher/findById?teacherId=" + id;
        System.out.println("url:"+url);
        Teacher teacher=new Teacher();
/*
        OkHttpClient okHttpClient = new OkHttpClient();
*/
        OkHttpClient okHttpClient = OkHttpUtils.getClient();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //接收数据
            teacher = gson.fromJson(string, Teacher.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return teacher;
    }


    /*
     * 修改密码
     * */
    public  void changePassword(Teacher teacher) {
        url = ipUrl + "/teacher/update?teacherPassword=" + teacher.getTeacherPassword()+"&teacherId="+teacher.getTeacherId();
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

    /*
     * 修改手机号
     * */
    public  void changePhone(Teacher teacher) {
        url = ipUrl + "/teacher/update?teacherPhone=" + teacher.getTeacherPhone()+"&teacherId="+teacher.getTeacherId();
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

    /*
     * 判断手机号是否已存在
     * */
    public  String existsByTeacherPhone(String phone) {
        url = ipUrl + "/teacher/existsByTeacherPhone?phone=" + phone;
        System.out.println("url:"+url);
        String result="";
        OkHttpClient okHttpClient = OkHttpUtils.getClient();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            result =response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }


    /*
public List<Teacher> findAll() {
    List<Teacher> list = new ArrayList<>();
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder().url("http://192.168.43.142:8080/teacher/findAll").get().build();
    Map<String, Object> resultMap = new HashMap<>();
    //执行异步get请求
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            System.out.println("失败");
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            String jsonRes = response.body().string();
            resultMap.put("json", jsonRes);
            System.out.println("json:"+resultMap.get("json"));
        }
    });
    //获得返回的字符串
    String string = (String) resultMap.get("json");
    System.out.println("string:"+string);
    //开始解析服务器传过来的json数据
    Gson gson = new Gson();
    //传过来的是list，需要用泛型来接收
    Type type = new TypeToken<List<Teacher>>() {}.getType();
    list = gson.fromJson(string, type);
    return list;
}
*/

}
